﻿#region CREDIT & LICENSE
/*******************************************
 * CREDIT: 
 * http://www.ROMPROS.com 
 * Tremendously helpful website, also would
 * like to give special thanks to:
 * 
 * Roverser - for the great effort on a c repository 
 * that most of this was ported from. http://code.google.com/p/rombot/
 * 
 * ROMeo - for the great amount of information shared
 * all across the rompros site. http://www.rompros.com/member.php?u=1901
 * 
 * 
 * LICENSE:
 *  
 * RomAPI is c# library is used to communicate directly with
 * the Runes Of Magic game client. 
 * 
 * Copyright (C) 2010  digitalxarts@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *******************************************/
#endregion
using System;
using System.Collections;
using System.Timers;
using System.Collections.Generic;
using System.Timers;
using System.Text;
using RomAPI.Offsets;
namespace RomAPI.Objects
{
    /// <summary>
    /// This delegate is for handling the Unit OnUpdate Event
    /// </summary>
    public delegate void RomWorldUpdateHandler(RomWorld world);
    /// <summary>
    /// World is an object that represents everything around the player.
    /// </summary>
    public class RomWorld : RomObject
    {
        /// <summary>
        /// Private Array to store all objects that currently in the 
        /// world around the player
        /// </summary>
        private RomUnit[] worldObjects;
        /// <summary>
        /// used to tell the updateTimer 
        /// how often it should refresh (default 500)
        /// </summary>
        private static int updateFrequency;
        /// <summary>
        /// used to track how much time is elapsed
        /// and force the Unit to refresh it's data.
        /// </summary>
        private Timer updateTimer;
        /// <summary>
        /// OnUpdate Event, this gets fired everytime updateFreqency is elapsed.
        /// </summary>
        public event RomWorldUpdateHandler OnUpdate;
        /// <summary>
        /// This is trigged by the elapsed time of UpdateFrequency,
        /// and used to force the object to refresh().
        /// </summary>
        protected void RomWorldUpdate(object sender, ElapsedEventArgs e)
        {
            //we must see if any events are registered,
            //if not then OnUpdate will be null.
            if (OnUpdate != null)
                OnUpdate(this);
        }
        /// <summary>
        /// Constructor - passes reference to client to parent (object)
        /// </summary>
        /// <param name="client"></param>
        public RomWorld(RomClient client)
            : base(client, (uint)RomWorldOffset.BASE)
        {
            //create an array that points to all the address in the world.
            worldObjects = new RomUnit[MaximumSize];
            for (int i = 0; i < MaximumSize; i++)
            {
                //move through the array of items
                worldObjects[i] = new RomUnit(client, (uint)(BaseAddress + (0x4 * i)));
            }
            updateFrequency = 100;
            updateTimer = new Timer(updateFrequency);
            updateTimer.Elapsed += new ElapsedEventHandler(RomWorldUpdate);
            updateTimer.Start();
            updateFrequency = 100;
            updateTimer = new Timer(updateFrequency);
            updateTimer.Elapsed += new ElapsedEventHandler(RomWorldUpdate);
            updateTimer.Start();
        }
        /// <summary>
        /// Read-Only access to world objects.
        /// </summary>
        public RomUnit[] WorldObjects
        {
            get { return worldObjects; }
        }
        /// <summary>
        /// Read-Only access to the current size of the environment.
        /// </summary>
        public uint CurrentSize
        {
            get { return GetPointerAddress((uint)RomWorldOffset.SIZE); }
        }
        /// <summary>
        /// Read-Only access to the maximum(64) size of the environment.
        /// </summary>
        public uint MaximumSize
        {
            get { return GetPointerAddress((uint)RomWorldOffset.MAX_SIZE); }
        }
    }
}
